package com.sangeng.controller;

import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.LoginUser;
import com.sangeng.domain.entity.User;
import com.sangeng.domain.vo.AdminUserInfoVo;
import com.sangeng.domain.vo.MenuVo;
import com.sangeng.domain.vo.RoutersVo;
import com.sangeng.domain.vo.UserInfoVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.handler.exception.SystemException;
import com.sangeng.service.LoginService;
import com.sangeng.service.MenuService;
import com.sangeng.service.RoleService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.models.Response;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: LoginController
 * Package: com.sangeng.controller
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/20 22:50
 * @Version 1.0
 */
@RestController
@Api(tags = "登录模块")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;
    

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
            if (!StringUtils.hasText(user.getUserName())){
                //提示 必须要传用户名
                throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
            }

            return loginService.login(user);
    }

    @GetMapping("/getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo(){
        //获取当前登录的用户
        LoginUser  loginUser = SecurityUtils.getLoginUser();

        //根据当前用户id查询权限信息
        List<String> perms=menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户id查询角色信息
        List<String> roleKeyList=roleService.selectRoleKeyByUserId(loginUser.getUser().getId());

        //获取用户信息
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);

        //封装数据返回
        AdminUserInfoVo adminUserInfoVo=new AdminUserInfoVo(perms,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    //动态路由信息的查询
    @GetMapping("/getRouters")
    public  ResponseResult getRouters() {
        //获取用户id
        Long  userId = SecurityUtils.getLoginUser().getUser().getId();
        List<MenuVo> menus = menuService.selectRoutersMenusByUserId(userId);
        return ResponseResult.okResult(new RoutersVo(menus));
    }

    //退出登录
    @PostMapping("/user/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }
}
